package com.ruoyi.bizsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.bizsys.domain.DeviceContactsRelative;
import com.ruoyi.bizsys.mapper.DeviceContactsRelativeMapper;
import com.ruoyi.bizsys.service.IDeviceContactsRelativeService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


/**
 * 用户通讯录关联Service业务层处理
 *
 * @author african-loan
 * @date 2021-12-10
 */
@Service
public class DeviceContactsRelativeServiceImpl extends ServiceImpl<DeviceContactsRelativeMapper,DeviceContactsRelative> implements IDeviceContactsRelativeService
{

    /**
     * 查询用户通讯录关联
     *
     * @param id 用户通讯录关联ID
     * @return 用户通讯录关联
     */
    @Override
    public DeviceContactsRelative selectDeviceContactsRelativeById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询用户通讯录关联列表
     *
     * @param deviceContactsRelative 用户通讯录关联
     * @return 用户通讯录关联
     */
    @Override
    public List<DeviceContactsRelative> selectDeviceContactsRelativeList(DeviceContactsRelative deviceContactsRelative) {
        return this.list(new QueryWrapper<>(deviceContactsRelative).last("order by id desc"));
    }

    /**
     * 新增用户通讯录关联
     *
     * @param deviceContactsRelative 用户通讯录关联
     * @return 结果
     */
            @Override
    public int insertDeviceContactsRelative(DeviceContactsRelative deviceContactsRelative) {
                                                                                                deviceContactsRelative.setCreateTime(DateUtils.getNowDate());
                            return baseMapper.insert(deviceContactsRelative);
    }

    /**
     * 修改用户通讯录关联
     *
     * @param deviceContactsRelative 用户通讯录关联
     * @return 结果
     */
            @Override
    public int updateDeviceContactsRelative(DeviceContactsRelative deviceContactsRelative) {
                                                                                                return baseMapper.updateById(deviceContactsRelative);
    }

    /**
     * 批量删除用户通讯录关联
     *
     * @param ids 需要删除的用户通讯录关联ID
     * @return 结果
     */
            @Override
    public int deleteDeviceContactsRelativeByIds(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除用户通讯录关联信息
     *
     * @param id 用户通讯录关联ID
     * @return 结果
     */
    @Override
    public int deleteDeviceContactsRelativeById(Long id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public String getTableNameByDeviceId(String deviceId) {
        QueryWrapper<DeviceContactsRelative> applyContactsQueryWrapper = new QueryWrapper<>();
        applyContactsQueryWrapper.eq("device_id",deviceId).last("limit 1");
        DeviceContactsRelative deviceContactsRelative = baseMapper.selectOne(applyContactsQueryWrapper);
        return deviceContactsRelative.getContactsTableName();
    }

    @Override
    public List<DeviceContactsRelative> getTableNamesByUserId(String userId) {
        return baseMapper.getTableNamesByUserId(userId);
    }

}
